# -*- coding: utf-8 -*-
"""
Created on Sat Apr 24 16:22:36 2021

@author: ikaros
"""

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 20 23:15:34 2020
@author: ikaros
"""
'''
该程序用于预处理数据集，由于弃用了EAST的dataset.py的crop_img函数，
用此文件处理完后才可以用EAST的train.py运行，
该文件主要用于把图片变成256*256的尺寸，有两种模式，
模式一针对卡片式子的图片，由于只有一个目标，直接resize成256*256.
模式二针对一页的算式图片，由于一页的尺寸w/h约等于1/2，故resize成256*512，再切成上下两半分成两张256*256的图。

主要的处理对象为模式二，
原来的EAST只要是文字就会当成目标，因此crop后目标区域的坐标变换方法不是很恰当，
一个字被切成两半也会被当成目标，引入了很大的误差，
但我们对式子完整性要求很高，因为完整性影响着后续的文字识别功能，不完整定位后的式子会使识别产生很大的错误，
于是，我这里以0.85位阈值，处于分界线的目标，被切成两半后如果还保留着超过0.85的部分就会被当成目标识别，
小于0.85的就认为该式子不完整，不能被当作正样本来训练。
'''

import numpy as np
import cv2
from PIL import Image
import math
import random
import os #路径有中文时可能会报一些奇怪的错
import time





def extract_vertices(lines):
	'''extract vertices info from txt lines --->txt的内容转化为数组类型的坐标
	Input:
		lines   : 标签文件里的所有内容
	Output:
		vertices: vertices of text regions <numpy.ndarray, (n,8)>
		labels  : 1->valid, 0->ignore, <numpy.ndarray, (n,)>
	'''
	labels = []
	vertices = []
	for line in lines:
		vertices.append(list(map(int,list(map(float,line.rstrip('\n').rstrip().lstrip('\ufeff').split(',')[:8])))))
		label = 0 if '###' in line else 1
		labels.append(label)
	return np.array(vertices), np.array(labels)

def area_compute(coordinate):
    '''
    用向量积计算四边形面积
    coordinate = np.array([x1,y1,x2,y2,x3,y3,x4,y4])
    '''
    coord = coordinate.reshape((4,2))
    
    vetor1 = coord[0] - coord[1]
    vetor2 = coord[0] - coord[3]
    
    vetor3 = coord[2] - coord[1]
    vetor4 = coord[2] - coord[3]
    area1 = abs(np.linalg.det([vetor1,vetor2]))*0.5 #第一个三角形的面积
    area2 = abs(np.linalg.det([vetor4,vetor3]))*0.5 #第二个三角形的面积
    
    result = area1 + area2
    return result

def mode_select(img):
    h,w = img.shape[0],img.shape[1]
    scale = h/w
    if scale > 1.3:                  #高长于宽很多
        mode = 1
    elif scale > 1 and scale < 1.3:  #高与宽差不多
        mode = 2
    else:                            
        if 1/scale >1.3:            #高短于宽很多
            mode = 3
        else:                       #高与宽差不多
            mode = 4
    return mode

def resize1_m1_m2(img, vertices, thread, img_name, save_img_dir, save_label_dir, times_to_crop,symbol_mode):
    '''
    高度比宽度长
    (1)h : w > 1.2 ---> h > 307
       把图切times_to_crop次，不一定是上下各一半
    (2)h : w < 1.2 ---> h < 307
       只裁剪一次
    '''
    img_w = img.shape[1]
    img_h = img.shape[0]
    scale = img_w /256
    img_h2 = round(img_h / scale)
    #vertices, label =extract_vertices(lines)
    
    img2 = cv2.resize(img,(256,img_h2))
    flag = 0        #统计成功生成了多少张图
    for num1 in range(times_to_crop):
        
        random_num1 = img_h2 - 257
        if random_num1 <0 :
            random_num1 = 0
        random_num2 = random.randint(0,random_num1)
        new_h0 = random_num2
        new_h1 = new_h0 +256
        
        img3 = img2[new_h0:new_h1,0:256]
        #print(img3.shape)
        new_lines = []
        
        for v1 in vertices:
            v1 = v1 / scale
            x1,y1,x2,y2,x3,y3,x4,y4 = v1[0],v1[1],v1[2],v1[3],v1[4],v1[5],v1[6],v1[7]
#            x1 = round(v1[0])
#            y1 = round(v1[1])
#            x2 = round(v1[2])
#            y2 = round(v1[3])
#            x3 = round(v1[4])
#            y3 = round(v1[5])
#            x4 = round(v1[6])
#            y4 = round(v1[7])
                        
            y = [y1,y2,y3,y4]
            y_min = min(y)
            y_max = max(y)
            #print(y)
            '''判断坐标点是否在子图内'''
            if (new_h1 - y_min < 0) or (y_max - new_h0 < 0):
                continue
            new_y1 = (y1 - new_h0) if (y1 - new_h0)>0 else 0
            new_y2 = (y2 - new_h0) if (y2 - new_h0)>0 else 0
            new_y3 = (y3 - new_h0) if (y3 - new_h0)>0 else 0
            new_y4 = (y4 - new_h0) if (y4 - new_h0)>0 else 0
            
            zero_num = 0
            for temp in [new_y1,new_y2,new_y3,new_y4]:
                if temp == 0:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图上方，则去除
                continue
            
            new_y1 = new_y1 if new_y1<255 else 255
            new_y2 = new_y2 if new_y2<255 else 255
            new_y3 = new_y3 if new_y3<255 else 255
            new_y4 = new_y4 if new_y4<255 else 255
            
            zero_num = 0
            for temp in [new_y1,new_y2,new_y3,new_y4]:
                if temp == 255:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图下方，则去除
                continue
            '''----------------------------------------------------------'''
            
            v2 = np.array([x1,new_y1,x2,new_y2,x3,new_y3,x4,new_y4])
            area1 = area_compute(v1)
            area2 = area_compute(v2)
            if abs(1 - (area2/area1)) > thread: #面积损失超过阈值的式子去除
                continue
            
            x1 = str(x1)
            x2 = str(x2)
            x3 = str(x3)
            x4 = str(x4)
            
            new_y1 = str(new_y1)
            new_y2 = str(new_y2)
            new_y3 = str(new_y3)
            new_y4 = str(new_y4)
            newline = x1 + ',' + new_y1 + ',' + x2 + ',' + new_y2 + ','+ x3 + ','+ new_y3 + ','+ x4 + ','+ new_y4 + ',' + 'A'+'\n' #A代表正样本
            #print('thred:',abs(1 - (area2/area1)))
            #print('newline:',newline)
            new_lines.append(newline)
        
        np_new_lines = np.array(new_lines)
        if np_new_lines.size != 0:
            symbol_txt = '('+ symbol_mode + '_num' +str(num1) +').txt'
            new_label = save_label_dir + 'gt_' + img_name.replace('.jpg',symbol_txt)
            with open(new_label, "a") as f1:         #把坐标和标签内容写入txt文件
                for ns in new_lines:
                    f1.write(ns)

            symbol_img = '('+ symbol_mode + '_num' +str(num1) +').jpg'
            saveimg_name = save_img_dir + img_name.replace('.jpg',symbol_img)
            cv2.imwrite(saveimg_name,img3)
            flag += 1
            print('save:',saveimg_name)
    
    return flag

def resize1_m3_m4(img, vertices, thread, img_name, save_img_dir, save_label_dir, times_to_crop,symbol_mode):
    '''
    宽比高长
    (1)w : h > 1.2 ---> w > 307
       把图切times_to_crop次，不一定是上下各一半
    (2)w : h < 1.2 ---> w < 307
       只裁剪一次
    '''
    img_w = img.shape[1]
    img_h = img.shape[0]
    scale = img_h /256
    img_w2 = round(img_w / scale)
    #vertices, label =extract_vertices(lines)
    
    img2 = cv2.resize(img,(img_w2,256))
    flag = 0        #统计成功生成了多少张图
    for num1 in range(times_to_crop):
        
        random_num1 = img_w2 - 257
        if random_num1 <0:
            random_num1 =0
        random_num2 = random.randint(0,random_num1)
        new_w0 = random_num2
        new_w1 = new_w0 +256
        
        img3 = img2[0:256,new_w0:new_w1]
        new_lines = []
        
        for v1 in vertices:
            v1 = v1 / scale
            x1,y1,x2,y2,x3,y3,x4,y4 = v1[0],v1[1],v1[2],v1[3],v1[4],v1[5],v1[6],v1[7]
#            x1 = round(v1[0])
#            y1 = round(v1[1])
#            x2 = round(v1[2])
#            y2 = round(v1[3])
#            x3 = round(v1[4])
#            y3 = round(v1[5])
#            x4 = round(v1[6])
#            y4 = round(v1[7])
            
            x = [x1,x2,x3,x4]
            x_min = min(x)
            x_max = max(x)
            o_w = x_max - x_min   
            y = [y1,y2,y3,y4]
            y_min = min(y)
            y_max = max(y)
            o_h = y_max - y_min
            
            '''判断坐标点是否在子图内'''
            if (new_w1 - x_min < 0) or (x_max - new_w0 < 0):
                continue
            new_x1 = (x1 - new_w0) if (x1 - new_w0)>0 else 0
            new_x2 = (x2 - new_w0) if (x2 - new_w0)>0 else 0
            new_x3 = (x3 - new_w0) if (x3 - new_w0)>0 else 0
            new_x4 = (x4 - new_w0) if (x4 - new_w0)>0 else 0
            
            zero_num = 0
            for temp in [new_x1,new_x2,new_x3,new_x4]:
                if temp == 0:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图左方，则去除
                continue
            
            new_x1 = new_x1 if new_x1<255 else 255
            new_x2 = new_x2 if new_x2<255 else 255
            new_x3 = new_x3 if new_x3<255 else 255
            new_x4 = new_x4 if new_x4<255 else 255
            
            zero_num = 0
            for temp in [new_x1,new_x2,new_x3,new_x4]:
                if temp == 255:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图右方，则去除
                continue
            '''----------------------------------------------------------'''
            
            v2 = np.array([new_x1,y1,new_x2,y2,new_x3,y3,new_x4,y4])
            area1 = area_compute(v1)
            area2 = area_compute(v2)
            if abs(1 - (area2/area1)) > thread: #面积损失超过阈值的式子去除
                #print('thred:',abs(1 - (area2/area1)))
                continue
            
            if o_w > 128:
                thred_x = thread / 2
                if (max([new_x1,new_x2,new_x3,new_x4])-min([new_x1,new_x2,new_x3,new_x4])) < o_w * (1-thred_x):
                    continue
                if (x_max - max([new_x1,new_x2,new_x3,new_x4])) > o_h/3 and (x_max - max([new_x1,new_x2,new_x3,new_x4])) < o_h:
                    continue
            
            y1 = str(y1)
            y2 = str(y2)
            y3 = str(y3)
            y4 = str(y4)
            
            new_x1 = str(new_x1)
            new_x2 = str(new_x2)
            new_x3 = str(new_x3)
            new_x4 = str(new_x4)
            newline = new_x1 + ',' + y1 + ',' + new_x2 + ','+ y2 + ','+ new_x3 + ','+ y3 + ','+ new_x4 + ',' + y4 + ',' + 'A'+'\n' #A代表正样本
            #print('newline:',newline)
            
            new_lines.append(newline)
        
        np_new_lines = np.array(new_lines)
        if np_new_lines.size != 0:
            symbol_txt = '('+ symbol_mode + '_num' +str(num1) +').txt'
            new_label = save_label_dir + 'gt_' + img_name.replace('.jpg',symbol_txt)
            with open(new_label, "a") as f1:         #把坐标和标签内容写入txt文件
                for ns in new_lines:
                    f1.write(ns)

            symbol_img = '('+ symbol_mode + '_num' +str(num1) +').jpg'
            saveimg_name = save_img_dir + img_name.replace('.jpg',symbol_img)
            cv2.imwrite(saveimg_name,img3)
            flag += 1
            print('save:',saveimg_name)
    
    return flag

def raw_crop(img, lines, thread, img_name, save_dir):
    '''
    先左切，再右切
    '''
#    temp_img_dir = save_dir + 'temp/images'
#    temp_label_dir = save_dir + 'temp/labels'
    save_img_dir = save_dir + 'temp/images/'
    save_label_dir = save_dir + 'temp/labels/'
    if not os.path.exists(save_img_dir):                 #创建保存目录
        os.makedirs(save_img_dir)
    if not os.path.exists(save_label_dir):                 #创建保存目录
        os.makedirs(save_label_dir)
    
    img_w = img.shape[1]
    #img_h = img.shape[0]
    vertices, label =extract_vertices(lines)
    img2 = img.copy()
    mode = mode_select(img2)
    if mode == 2 or mode ==4: #只裁剪长图
        return None
    flag = 0
    for num_type in range(4):
#        for num1 in range(times_to_crop):
        if num_type == 0: #3/4的左侧
            range_w1 = int(img_w/7)
            range_w2 = int(img_w/4)-2
            img_w2 = int(img_w*0.75)
            random_num2 = random.randint(range_w1,range_w2)
            #print('random_num2:', random_num2)
            
            new_w0 = random_num2
            new_w1 = new_w0 +img_w2
            #img3 = img2[new_w0:new_w1,:]
            img3 = img2[:,new_w0:new_w1]
            
        elif num_type == 1: #3/4的右侧
            img_w2 = int(img_w*0.75)
            range_w1 = img_w2 + 1
            range_w2 = img_w - int(img_w/9)
            random_num2 = random.randint(range_w1,range_w2)
            #print('random_num2:', random_num2)
            
            new_w1 = random_num2
            new_w0 = new_w1 - img_w2
            img3 = img2[:,new_w0:new_w1]
            
        elif num_type == 2: #1/2左侧
            img_w2 = int(img_w*0.5)
            img3 = img2[:,0:img_w2]
            new_w0 = 0
            new_w1 = img_w2 -1
        
        elif num_type == 3: #1/2右侧
            img_w2 = int(img_w*0.5)
            img3 = img2[:,img_w2:]
            new_w0 = img_w2
            new_w1 = img_w -1
            
        
        new_lines = []
        img_w3 = img3.shape[1]
        for v1 in vertices:
            x1,y1,x2,y2,x3,y3,x4,y4 = v1[0],v1[1],v1[2],v1[3],v1[4],v1[5],v1[6],v1[7]
            
            x = [x1,x2,x3,x4]
            x_min = min(x)
            x_max = max(x)
            o_w = x_max - x_min 
            y = [y1,y2,y3,y4]
            y_min = min(y)
            y_max = max(y)
            o_h = y_max - y_min
            
            '''判断坐标点是否在子图内'''
            if (new_w1 - x_min < 0) or (x_max - new_w0 < 0):
                continue
            new_x1 = (x1 - new_w0) if (x1 - new_w0)>0 else 0
            new_x2 = (x2 - new_w0) if (x2 - new_w0)>0 else 0
            new_x3 = (x3 - new_w0) if (x3 - new_w0)>0 else 0
            new_x4 = (x4 - new_w0) if (x4 - new_w0)>0 else 0
            
            zero_num = 0
            for temp in [new_x1,new_x2,new_x3,new_x4]:
                if temp == 0:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图左方，则去除
                continue
            
            new_x1 = new_x1 if new_x1<img_w3 else (img_w3-1)
            new_x2 = new_x2 if new_x2<img_w2 else (img_w3-1)
            new_x3 = new_x3 if new_x3<img_w2 else (img_w3-1)
            new_x4 = new_x4 if new_x4<img_w2 else (img_w3-1)
            
            zero_num = 0
            for temp in [new_x1,new_x2,new_x3,new_x4]:
                if temp == (img_w3-1):
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图右方，则去除
                continue
            '''----------------------------------------------------------'''
            
            v2 = np.array([new_x1,y1,new_x2,y1,new_x3,y3,new_x4,y4])
            area1 = area_compute(v1)
            area2 = area_compute(v2)
            if abs(1 - (area2/area1)) > thread: #面积损失超过阈值的式子去除
                continue
            if o_w > 128:
                thred_x = thread / 2
                if (max([new_x1,new_x2,new_x3,new_x4])-min([new_x1,new_x2,new_x3,new_x4])) < o_w * (1-thred_x):
                    continue
                if (x_max - max([new_x1,new_x2,new_x3,new_x4])) > o_h/3 and (x_max - max([new_x1,new_x2,new_x3,new_x4])) < o_h:
                    continue
            
            y1 = str(y1)
            y2 = str(y2)
            y3 = str(y3)
            y4 = str(y4)
            
            new_x1 = str(new_x1)
            new_x2 = str(new_x2)
            new_x3 = str(new_x3)
            new_x4 = str(new_x4)
            newline = new_x1 + ',' + y1 + ',' + new_x2 + ','+ y2 + ','+ new_x3 + ','+ y3 + ','+ new_x4 + ',' + y4 + ',' + 'A'+'\n' #A代表正样本
            new_lines.append(newline)
            
        np_new_lines = np.array(new_lines)
        if np_new_lines.size != 0:
            #symbol_txt = '('+str(num_type)+'raw_num' +str(num1) +').txt'
            symbol_txt = '('+str(num_type)+'raw_num' +').txt'
            new_label = save_label_dir + 'gt_' + img_name.replace('.jpg',symbol_txt)
            with open(new_label, "a") as f1:         #把坐标和标签内容写入txt文件
                for ns in new_lines:
                    f1.write(ns)

            #symbol_img = '('+str(num_type)+'raw_num' +str(num1) +').jpg'
            symbol_img = '('+str(num_type)+'raw_num' +').jpg'
            saveimg_name = save_img_dir + img_name.replace('.jpg',symbol_img)
            cv2.imwrite(saveimg_name,img3)
            flag += 1
            print('save:',saveimg_name)
    return flag

def resize3_crop(img, vertices, thread, img_name, save_img_dir, save_label_dir, times_to_crop,symbol_mode):

    #vertices, label =extract_vertices(lines)
    num_vertices_size = vertices.shape[0] - 1        #目标数量
    v_w_max = 0
    img_w = img.shape[1]
    img_h = img.shape[0]
    for v1 in vertices:
        x1,y1,x2,y2,x3,y3,x4,y4 = v1[0],v1[1],v1[2],v1[3],v1[4],v1[5],v1[6],v1[7]
        x = [x1,x2,x3,x4]
        x_min = min(x)
        x_max = max(x)
        v_w1 = x_max - x_min
        
        if v_w1 > 256 and v_w1 > v_w_max:
            v_w_max = v_w1
    if v_w_max > 256:
        scale = v_w_max/260
        img_h2 = round(img_h / scale)
        img_w2 = round(img_w / scale)
    else:
        scale = 1
        img_h2 = img_h
        img_w2 = img_w 
    
    
    
    img2 = cv2.resize(img,(img_w2,img_h2))
    
    flag = 0        #统计成功生成了多少张图
    
    times_to_crop += 2
    for num1 in range(times_to_crop):
        num2 = 0
        random_index1 = random.randint(0,num_vertices_size)
        v1 = vertices[random_index1]
        v1 = v1 / scale
        
        x1 = round(v1[0])
        y1 = round(v1[1])
        x2 = round(v1[2])
        y2 = round(v1[3])
        x3 = round(v1[4])
        y3 = round(v1[5])
        x4 = round(v1[6])
        y4 = round(v1[7])
        x = [x1,x2,x3,x4]
        x_min = min(x)
        x_max = max(x)
#        o_w = x_max - x_min
        y = [y1,y2,y3,y4]
        y_min = min(y)
        y_max = max(y)
#        o_h = y_max - y_min
        
        x_center = int((x_max+x_min) /2)
        y_center = int((y_max+y_min) /2)
        
        random_num1 = random.randint(0,256)
        random_num2 = random.randint(0,256)
        img_x0 = x_center - random_num1
        img_x1 = img_x0 + 256
        img_y0 = y_center - random_num2
        img_y1 = img_y0 + 256
        if img_y1 > img_h2-1 or img_y0 < 0 or img_x0<0 or img_x1 > img_w2-1:
            continue
        
        img3 = img2[img_y0:img_y1,img_x0:img_x1]
        new_lines = []
        for v2 in vertices:
            v2 = v2 / scale
            x1 = round(v2[0])
            y1 = round(v2[1])
            x2 = round(v2[2])
            y2 = round(v2[3])
            x3 = round(v2[4])
            y3 = round(v2[5])
            x4 = round(v2[6])
            y4 = round(v2[7])
            
            x = [x1,x2,x3,x4]
            x_min = min(x)
            x_max = max(x)
            o_w = x_max - x_min 
            y = [y1,y2,y3,y4]
            y_min = min(y)
            y_max = max(y)
            o_h = y_max - y_min 
            
            '''判断坐标点是否在子图内'''
            if (img_x1 - x_min < 0) or (x_max - img_x0 < 0):
                continue
            if (img_y1 - y_min < 0) or (y_max - img_y0 < 0):
                continue
            new_x1 = (x1 - img_x0) if (x1 - img_x0)>0 else 0
            new_x2 = (x2 - img_x0) if (x2 - img_x0)>0 else 0
            new_x3 = (x3 - img_x0) if (x3 - img_x0)>0 else 0
            new_x4 = (x4 - img_x0) if (x4 - img_x0)>0 else 0
            
            zero_num = 0
            for temp in [new_x1,new_x2,new_x3,new_x4]:
                if temp == 0:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图左方，则去除
                continue
            
            new_x1 = new_x1 if new_x1<255 else 255
            new_x2 = new_x2 if new_x2<255 else 255
            new_x3 = new_x3 if new_x3<255 else 255
            new_x4 = new_x4 if new_x4<255 else 255
            
            zero_num = 0
            for temp in [new_x1,new_x2,new_x3,new_x4]:
                if temp == 255:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图右方，则去除
                continue

            new_y1 = (y1 - img_y0) if (y1 - img_y0)>0 else 0
            new_y2 = (y2 - img_y0) if (y2 - img_y0)>0 else 0
            new_y3 = (y3 - img_y0) if (y3 - img_y0)>0 else 0
            new_y4 = (y4 - img_y0) if (y4 - img_y0)>0 else 0
            
            zero_num = 0
            for temp in [new_y1,new_y2,new_y3,new_y4]:
                if temp == 0:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图上方，则去除
                continue
            
            new_y1 = new_y1 if new_y1<255 else 255
            new_y2 = new_y2 if new_y2<255 else 255
            new_y3 = new_y3 if new_y3<255 else 255
            new_y4 = new_y4 if new_y4<255 else 255
            
            zero_num = 0
            for temp in [new_y1,new_y2,new_y3,new_y4]:
                if temp == 255:
                    zero_num +=1
            if zero_num > 2:                    #有3个点都在子图下方，则去除
                continue
            '''-----------------------------------------------------------'''
            
            v3 = np.array([new_x1,new_y1,new_x2,new_y2,new_x3,new_y3,new_x4,new_y4])
            area1 = area_compute(v2)
            area2 = area_compute(v3)
            
            if abs(1 - (area2/area1)) > thread: #面积损失超过阈值的式子去除
                continue
            if o_w > 128:
                thred_x = thread / 4
                if (max([new_x1,new_x2,new_x3,new_x4])-min([new_x1,new_x2,new_x3,new_x4])) < o_w * (1-thred_x):
                    continue
                if (x_max - max([new_x1,new_x2,new_x3,new_x4]) - img_x0) > o_h*0.2 and (x_max - max([new_x1,new_x2,new_x3,new_x4]) - img_x0) < o_h*0.5:
                    continue
                if abs(x_min - min([new_x1,new_x2,new_x3,new_x4])-img_x0) > o_h*0.15 :
                    continue
            
            new_x1 = str(new_x1)
            new_x2 = str(new_x2)
            new_x3 = str(new_x3)
            new_x4 = str(new_x4)
            
            new_y1 = str(new_y1)
            new_y2 = str(new_y2)
            new_y3 = str(new_y3)
            new_y4 = str(new_y4)
            newline = new_x1 + ',' + new_y1 + ',' + new_x2 + ','+ new_y2 + ','+ new_x3 + ','+ new_y3 + ','+ new_x4 + ',' + new_y4 + ',' + 'A'+'\n' #A代表正样本
            new_lines.append(newline)
        
        np_new_lines = np.array(new_lines)
        if np_new_lines.size != 0:
            symbol_txt = '('+ symbol_mode + '_num' +str(num1) +str(num2) +').txt'
            new_label = save_label_dir + 'gt_' + img_name.replace('.jpg',symbol_txt)
            with open(new_label, "a") as f1:         #把坐标和标签内容写入txt文件
                for ns in new_lines:
                    f1.write(ns)

            symbol_img = '('+ symbol_mode + '_num' +str(num1) +str(num2) +').jpg'
            saveimg_name = save_img_dir + img_name.replace('.jpg',symbol_img)
            cv2.imwrite(saveimg_name,img3)
            num2 += 1
            flag += 1
            print('save:',saveimg_name)
    return flag

def up_down_crop(type1,img,vertices,thread):

    img_w = img.shape[1]
    img_h = img.shape[0]
    
    img2 = img.copy()
    
    
    if img_h*0.5 < img_w:
        range_h1 = img_h*0.5
        range_h2 = img_w
    elif img_h*0.5 > img_w:
        range_h2 = img_h*0.5
        range_h1 = img_w
    rangenum = range_h2 - range_h1 -1
    random_num = random.randint(0,rangenum)
    
    if type1 == 'up':
        new_h0 = random_num
        new_h1 = range_h2 + random_num
    elif type1 == 'down':
        new_h1 = img_h - random_num
        new_h0 = new_h1 - range_h2
    
    img3 = img2[new_h0:new_h1,:]
    img_h2 = new_h1-new_h0
    new_vertices = []
    for v1 in vertices:
        x1,y1,x2,y2,x3,y3,x4,y4 = v1[0],v1[1],v1[2],v1[3],v1[4],v1[5],v1[6],v1[7]
        
#        x = [x1,x2,x3,x4]
#        x_min = min(x)
#        x_max = max(x)
#        o_w = x_max - x_min 
        y = [y1,y2,y3,y4]
        y_min = min(y)
        y_max = max(y)
#        o_h = y_max - y_min 
        
        '''判断坐标点是否在子图内'''
        if (new_h1 - y_min < 0) or (y_max - new_h0 < 0):
            continue
        new_y1 = (y1 - new_h0) if (y1 - new_h0)>0 else 0
        new_y2 = (y2 - new_h0) if (y2 - new_h0)>0 else 0
        new_y3 = (y3 - new_h0) if (y3 - new_h0)>0 else 0
        new_y4 = (y4 - new_h0) if (y4 - new_h0)>0 else 0
        
        zero_num = 0
        for temp in [new_y1,new_y2,new_y3,new_y4]:
            if temp == 0:
                zero_num +=1
        if zero_num > 2:                    #有3个点都在子图上方，则去除
            continue
        
        new_y1 = new_y1 if new_y1<img_h2 else (img_h2-1)
        new_y2 = new_y2 if new_y2<img_h2 else (img_h2-1)
        new_y3 = new_y3 if new_y3<img_h2 else (img_h2-1)
        new_y4 = new_y4 if new_y4<img_h2 else (img_h2-1)
        
        zero_num = 0
        for temp in [new_y1,new_y2,new_y3,new_y4]:
            if temp == (img_h2-1):
                zero_num +=1
        if zero_num > 2:                    #有3个点都在子图下方，则去除
            continue
        '''----------------------------------------------------------'''
        
        v2 = np.array([x1,new_y1,x2,new_y2,x3,new_y3,x4,new_y4])
        area1 = area_compute(v1)
        area2 = area_compute(v2)
        if abs(1 - (area2/area1)) > thread: #面积损失超过阈值的式子去除
            continue
        
#        if o_w > 128:
#            thred_x = thread / 2
#            if (max([new_x1,new_x2,new_x3,new_x4])-min([new_x1,new_x2,new_x3,new_x4])) < o_w * (1-thred_x):
#                continue
#            if (x_max - max([new_x1,new_x2,new_x3,new_x4])) > o_h/3 and (x_max - max([new_x1,new_x2,new_x3,new_x4])) < o_h:
#                continue
        
        new_vertices.append(v2)
        
    return img3, np.array(new_vertices)


def mode1_crop(o_img_dir,o_label_dir,save_img_dir,save_label_dir,times_to_crop,thread):
    '''
    全图缩放至256*256
    mode1:把图切分成上下两半
    mode2:把图按宽度缩放为256*h，再切成256*256
    mode3:把图
    '''
    save_img_dir = save_img_dir + 'mode1/'
    save_label_dir = save_label_dir + 'mode1/'
    if not os.path.exists(save_img_dir):                 #创建保存目录
        os.makedirs(save_img_dir)
    if not os.path.exists(save_label_dir):                 #创建保存目录
        os.makedirs(save_label_dir)
    
    imgslist = os.listdir(o_img_dir)
    for imgname in imgslist:
        imgdir = o_img_dir + imgname
        img = cv2.imread(imgdir)
        #img_np = np.array(img)
        
        labelname = o_label_dir + 'gt_' + imgname.replace('.jpg','.txt')
        #save_labelname = save_labeldir + imgname.split('.')[0]+'(m1).txt'
        with open(labelname, "r") as f1:                #读取标签文件的坐标和标签
            lines = f1.readlines()
        vertices, label =extract_vertices(lines)
        img2 =img.copy()
        mode = mode_select(img2)
        print(imgname,' mode: ',mode)
        if mode == 1:
            symbol_mode = 'mode1_m1'
            resize1_m1_m2(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
        elif mode == 2:
            symbol_mode = 'mode1_m2'
            times_to_crop = 1 #默认只裁剪一次
            resize1_m1_m2(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
        elif mode == 3:
            symbol_mode = 'mode1_m3'
            resize1_m3_m4(img2, vertices, thread, imgname, save_img_dir, save_label_dir,  times_to_crop, symbol_mode)
        elif mode == 4:
            symbol_mode = 'mode1_m4'
            times_to_crop = 1 #默认只裁剪一次
            resize1_m3_m4(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
    return save_img_dir, save_label_dir

def mode2_crop(root_dir,o_img_dir,o_label_dir,save_img_dir,save_label_dir,times_to_crop,thread):
    '''
    按宽度的3/4截图后再用mode1_crop()裁剪。
    '''
    temp_img_dir = root_dir + 'temp/images/'
    temp_label_dir = root_dir + 'temp/labels/'
    save_img_dir = save_img_dir + 'mode2/'
    save_label_dir = save_label_dir + 'mode2/'
    if not os.path.exists(save_img_dir):                 #创建保存目录
        os.makedirs(save_img_dir)
    if not os.path.exists(save_label_dir):                 #创建保存目录
        os.makedirs(save_label_dir)
        
    imgslist = os.listdir(o_img_dir)
    
    for imgname in imgslist:
        imgdir = o_img_dir + imgname
        img = cv2.imread(imgdir)
        if img is None:
            print(imgname,':None')
            exit(0)
        #img_np = np.array(img)
        
        labelname = o_label_dir + 'gt_' + imgname.replace('.jpg','.txt')
        #save_labelname = save_labeldir + imgname.split('.')[0]+'(m1).txt'
        with open(labelname, "r") as f1:                #读取标签文件的坐标和标签
            lines = f1.readlines()
        img2 =img.copy()
        raw_crop(img2, lines, thread, imgname, root_dir)
        
        
    imgslist2 = os.listdir(temp_img_dir)
    for imgname in imgslist2:
        imgdir = temp_img_dir + imgname
        img = cv2.imread(imgdir)
        #img_np = np.array(img)
        
        labelname = temp_label_dir + 'gt_' + imgname.replace('.jpg','.txt')
        with open(labelname, "r") as f1:                #读取标签文件的坐标和标签
            lines = f1.readlines()
        vertices, label =extract_vertices(lines)
        
        img2 =img.copy()
        mode = mode_select(img2)
        if mode == 1:
            symbol_mode = 'mode2_m1'
            resize1_m1_m2(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
            #print('???')
        elif mode == 2:
            symbol_mode = 'mode2_m2'
            times_to_crop = 1 #默认只裁剪一次
            resize1_m1_m2(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
        elif mode == 3:
            symbol_mode = 'mode2_m3'
            resize1_m3_m4(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
        elif mode == 4:
            symbol_mode = 'mode2_m4'
            times_to_crop = 1 #默认只裁剪一次
            resize1_m3_m4(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
        
#    if os.path.exists(temp1): #删除中间缓存文件
#        os.remove(temp1)
#    if os.path.exists(temp2):
#        os.remove(temp2)
    return save_img_dir, save_label_dir
    

def mode3_crop(root_dir,o_img_dir,o_label_dir,save_img_dir,save_label_dir,times_to_crop,thread):
    '''
    用于制作较大的目标数据。
    线判断图中目标最长的的长度，
    然后把目标的长度压缩至256以内，再直接截取子图。
    '''
    save_img_dir = save_img_dir + 'mode3/'
    save_label_dir = save_label_dir + 'mode3/'
    if not os.path.exists(save_img_dir):                 #创建保存目录
        os.makedirs(save_img_dir)
    if not os.path.exists(save_label_dir):                 #创建保存目录
        os.makedirs(save_label_dir)
        
    imgslist = os.listdir(o_img_dir)
    for num in range(4):
        time1 = time.time()
        for imgname in imgslist:
            imgdir = o_img_dir + imgname
            img = cv2.imread(imgdir)
            #img_np = np.array(img)
            
            labelname = o_label_dir + 'gt_' + imgname.replace('.jpg','.txt')
            #save_labelname = save_labeldir + imgname.split('.')[0]+'(m1).txt'
            with open(labelname, "r") as f1:                #读取标签文件的坐标和标签
                lines = f1.readlines()
            img2 =img.copy()
            vertices, label =extract_vertices(lines)
            
            if num == 0:
                symbol_mode = 'mode3_m1'
                resize3_crop(img2, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop,symbol_mode)
            elif num == 1:
                img_w = img2.shape[1]
                img_h = img2.shape[0]
                img_w2 = 512
                scale = img_w / 512
                img_h2 = round(img_h / scale)
                img3 = cv2.resize(img2,(img_w2,img_h2))
                vertices = vertices /scale
                symbol_mode = 'mode3_m2'
                resize3_crop(img3, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop,symbol_mode)
            elif num == 2:
                img_w = img2.shape[1]
                img_h = img2.shape[0]
                img_w2 = 1024
                scale = img_w / 1024
                img_h2 = round(img_h / scale)
                img3 = cv2.resize(img2,(img_w2,img_h2))
                vertices = vertices /scale
                symbol_mode = 'mode3_m3'
                resize3_crop(img3, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop,symbol_mode)
            elif num == 3:
                img_w = img2.shape[1]
                img_h = img2.shape[0]
                img_w2 = 2560
                scale = img_w / 2560
                img_h2 = round(img_h / scale)
                img3 = cv2.resize(img2,(img_w2,img_h2))
                vertices = vertices /scale
                symbol_mode = 'mode3_m4'
                resize3_crop(img3, vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop,symbol_mode)
        time2 = time.time()
        print('---------------------------')
        print('---------------------------')
        print('---------------------------')
        print('time cost:', time2-time1)
    return save_img_dir, save_label_dir

def mode4_crop(o_img_dir,o_label_dir,save_img_dir,save_label_dir,thread,times_to_crop =1):
    '''
    先把图片上下裁剪为两部分，再压缩再裁剪
    默认只裁剪1次
    与函数mode1_crop()的操作差不多，但不要横向的大图，只要纵向的大图
    '''
    types = ['up','down']
    save_img_dir = save_img_dir + 'mode4/'
    save_label_dir = save_label_dir + 'mode4/'
    if not os.path.exists(save_img_dir):                 #创建保存目录
        os.makedirs(save_img_dir)
    if not os.path.exists(save_label_dir):                 #创建保存目录
        os.makedirs(save_label_dir)
    
    imgslist = os.listdir(o_img_dir)
    for imgname in imgslist:
        time1 = time.time()
        imgdir = o_img_dir + imgname
        img = cv2.imread(imgdir)
        img2 =img.copy()
        mode = mode_select(img2)
        if mode == 2 or mode ==4 or mode == 3: #只裁剪长图
            continue
        labelname = o_label_dir + 'gt_' + imgname.replace('.jpg','.txt')
        #save_labelname = save_labeldir + imgname.split('.')[0]+'(m1).txt'
        with open(labelname, "r") as f1:                #读取标签文件的坐标和标签
            lines = f1.readlines()
        vertices, label =extract_vertices(lines)
        for type1 in types:
            img3, new_vertices = up_down_crop(type1,img2,vertices,thread)
            symbol_mode = 'mode4_m1'+ type1
            resize1_m1_m2(img3, new_vertices, thread, imgname, save_img_dir, save_label_dir, times_to_crop, symbol_mode)
        time2 = time.time()
        print('---------------------------')
        print('---------------------------')
        print('---------------------------')
        print('time cost:', time2-time1)
    return save_img_dir, save_label_dir
'''------------------------------------------------------------------------------------'''
'''---------------------------------简化的粗暴式裁剪------------------------------------'''
def v_normalization(img_v,vertices, scale_w,scale_h, thread):
    '''
    img_v:[x_min,x_max,y_min,y_max]
    '''
    img_x_min, img_x_max,img_y_min,img_y_max = img_v[0],img_v[1],img_v[2],img_v[3]
    img_w = img_y_max - img_y_min
    img_h = img_y_max - img_y_min
    new_vertices2 = []
    new_vertices3 = []
    for v1 in vertices:
        x1,y1,x2,y2,x3,y3,x4,y4 = v1[0],v1[1],v1[2],v1[3],v1[4],v1[5],v1[6],v1[7]
        
        x = [x1,x2,x3,x4]
        x_min = min(x)
        x_max = max(x)
        o_w = x_max - x_min 
        y = [y1,y2,y3,y4]
        y_min = min(y)
        y_max = max(y)
        o_h = y_max - y_min 
        
        '''判断坐标点是否在子图内'''
        if (x_min > img_x_max) or (x_max < img_x_min):
            continue
        if (y_min > img_y_max) or (y_max < img_y_min):
            continue
        
        new_y1 = (y1 - img_y_min) if (y1 > img_y_min) else 0
        new_y2 = (y2 - img_y_min) if (y2 > img_y_min) else 0
        new_y3 = (y3 - img_y_min) if (y3 > img_y_min) else 0
        new_y4 = (y4 - img_y_min) if (y4 > img_y_min) else 0
        
        new_x1 = (x1 - img_x_min) if (x1 > img_x_min) else 0
        new_x2 = (x2 - img_x_min) if (x2 > img_x_min) else 0
        new_x3 = (x3 - img_x_min) if (x3 > img_x_min) else 0
        new_x4 = (x4 - img_x_min) if (x4 > img_x_min) else 0
        
        zero_num = 0
        for temp in [new_y1,new_y2,new_y3,new_y4]:
            if temp == 0:
                zero_num +=1
        if zero_num > 2:                    #有3个点都在子图上方，则去除
            continue
        zero_num = 0
        for temp in [new_x1,new_x2,new_x3,new_x4]:
            if temp == 0:
                zero_num +=1
        if zero_num > 2:                    #有3个点都在子图左方，则去除
            continue
        
        new_y1 = new_y1 if y1<img_y_max else (img_h-1)
        new_y2 = new_y2 if y2<img_y_max else (img_h-1)
        new_y3 = new_y3 if y3<img_y_max else (img_h-1)
        new_y4 = new_y4 if y4<img_y_max else (img_h-1)
        
        new_x1 = new_x1 if x1<img_x_max else (img_w-1)
        new_x2 = new_x2 if x2<img_x_max else (img_w-1)
        new_x3 = new_x3 if x3<img_x_max else (img_w-1)
        new_x4 = new_x4 if x4<img_x_max else (img_w-1)
        
        zero_num = 0
        for temp in [new_y1,new_y2,new_y3,new_y4]:
            if temp == (img_h-1):
                zero_num +=1
        if zero_num > 2:                    #有3个点都在子图下方，则去除
            continue
        zero_num = 0
        for temp in [new_x1,new_x2,new_x3,new_x4]:
            if temp == (img_w-1):
                zero_num +=1
        if zero_num > 2:                    #有3个点都在子图右方，则去除
            continue
        '''----------------------------------------------------------'''
        
        v2 = np.array([new_x1,new_y1,new_x2,new_y2,new_x3,new_y3,new_x4,new_y4])
        area1 = area_compute(v1)
        area2 = area_compute(v2)
        if abs(1 - (area2/area1)) > thread: #面积损失超过阈值的式子去除
            continue
        
#        if o_w > img_w*0.5:
#            thred_x = thread / 2
#            if (max([new_x1,new_x2,new_x3,new_x4])-min([new_x1,new_x2,new_x3,new_x4])) < o_w * (1-thred_x):
#                continue
#            if (x_max - max([new_x1,new_x2,new_x3,new_x4]-img_x_min)) > o_h/3 and (x_max - max([new_x1,new_x2,new_x3,new_x4])-img_x_min) < o_h:
#                continue
        
        v3 = []
        for a in range(v2.shape[0]):
            b = None
            if a%2:
                b = v2[a]/scale_h
            else:
                b = v2[a]/scale_w
            if b is not None:
                v3.append(b)
        new_vertices2.append(v2)
        new_vertices3.append(v3)
    return np.array(new_vertices2),np.array(new_vertices3)
        
def write_new_label(labelname,new_vertices):
    if new_vertices.size == 0:
        return None
    newlines = []
    for coord_index in range(new_vertices.shape[0]):
        newline = ''
        for s in new_vertices[coord_index]:
            newline += str(s) 
            newline += ','
        newline = newline + 'A'+'\n'
        newlines.append(newline)
    with open(labelname, "a") as f1:         #把坐标和标签内容写入txt文件
        for ns in newlines:
            f1.write(ns)


def simple_crop(o_img_dir,o_label_dir,save_img_dir,save_label_dir,thread,times_to_crop =1):
    '''
    宽比高长的，只截取中间部分。
    高比宽长的，把图片均分成两部分。
    '''
    save_img_dir = save_img_dir + 's_mode/'
    save_label_dir = save_label_dir + 's_mode/'
    if not os.path.exists(save_img_dir):                 #创建保存目录
        os.makedirs(save_img_dir)
    if not os.path.exists(save_label_dir):                 #创建保存目录
        os.makedirs(save_label_dir)
    
    imgslist = os.listdir(o_img_dir)
    
    for imgname in imgslist:
        time1 = time.time()
        labelname = o_label_dir + 'gt_' + imgname.replace('.jpg','.txt')
        #save_labelname = save_labeldir + imgname.split('.')[0]+'(m1).txt'
        with open(labelname, "r") as f1:                #读取标签文件的坐标和标签
            lines = f1.readlines()
        vertices, label =extract_vertices(lines)
        imgdir = o_img_dir + imgname
        img = cv2.imread(imgdir)
        w,h = img.shape[1],img.shape[0]
        if w>h:
            img_x0 = int(w/2) - int(h/2)
            img_x1 = int(w/2) + int(h/2)
            img2 = img[:,img_x0:img_x1]
            y0 = 0
            y1 = h-1
            scale = h/256
            img_v = np.array([img_x0,img_x1,y0,y1])
            new_vertices2,new_vertices3 = v_normalization(img_v, vertices,scale,scale, thread)
            labelname = save_label_dir + 'gt_' + imgname.replace('.jpg','s_m1.txt')
            saveimg_name = save_img_dir + imgname.replace('.jpg','s_m1.jpg')
            img3 = cv2.resize(img2,(256,256))
            cv2.imwrite(saveimg_name,img3)
            write_new_label(labelname,new_vertices3)
            
        else:
            img_y0 = int(h/2)-1
            img_y1 = h-1
            up_img = img[0:img_y0,:]
            down_img = img[img_y0:img_y1,:]
            yup_0 = 0
            yup_1 = img_y0
            ydown_1 = img_y1
            img_vup = np.array([0,w-1,yup_0,yup_1])
            
            up_h = up_img.shape[0]
            up_w = up_img.shape[1]
            scale_h_up = up_h/256
            scale_w_up = up_w/256
            up_img2 = cv2.resize(up_img,(256,256))
            new_vertices_up2,new_vertices_up3 = v_normalization(img_vup,vertices, scale_w_up,scale_h_up, thread)
            labelname_up = save_label_dir + 'gt_' + imgname.replace('.jpg','s_mup.txt')
            saveimg_name = save_img_dir + imgname.replace('.jpg','s_mup.jpg')
            cv2.imwrite(saveimg_name,up_img2)
            write_new_label(labelname_up,new_vertices_up3)
            
            down_h = down_img.shape[0]
            down_w = down_img.shape[1]
            scale_h_down = down_h /256
            scale_w_down = down_w /256
            down_img2 = cv2.resize(down_img,(256,256))
            img_vdown = np.array([0,w-1,yup_1,ydown_1])
            new_vertices_down2,new_vertices_down3 = v_normalization(img_vdown, vertices,scale_w_down,scale_h_down, thread)
            labelname_down = save_label_dir + 'gt_' + imgname.replace('.jpg','s_mdown.txt')
            saveimg_name = save_img_dir + imgname.replace('.jpg','s_mdown.jpg')
            cv2.imwrite(saveimg_name,down_img2)
            write_new_label(labelname_down,new_vertices_down3)
        time2 = time.time()
        print('---------------------------')
        print('---------------------------')
        print('time cost:', time2-time1)
        print(saveimg_name)
    return save_img_dir, save_label_dir


'''-----------------------------------------------------------------------------------------------------------------'''

def verticle_repair(labeldir,label_savedir2):
    '''
    upon_names:  类型为list，用mode2_resize处理后的所有upon_name存到这个列表里
    below_names: 类型为list，用mode2_resize处理后的所有below_name存到这个列表里
    savedir2:    保存新的修正坐标后的label文件，地址要与upon_name、below_name的根目录不同
    '''
    labellist = os.listdir(labeldir)
    for labelname in labellist:
        read_label = labeldir + labelname
        with open(read_label, "r") as f1:            #读取标签文件的坐标和标签
            lines = f1.readlines()
        vertices, label3 = extract_vertices(lines)  #提取坐标
        newlines=[]
        for v1 in vertices:                         #判断坐标y是否超出图片范围，超出就修正回来
            if v1 is None:
                print('None')
                break
            x1 = str(int(round(v1[0])))
            y1 = round(v1[1])
            x2 = str(int(round(v1[2])))
            y2 = round(v1[3])
            x3 = str(int(round(v1[4])))
            y3 = round(v1[5])
            x4 = str(int(round(v1[6])))
            y4 = round(v1[7])
            
            y = [y1,y2,y3,y4]
            y_min = min(y)
            y_max = max(y)
            h_min = y_max - y_min
            if h_min < 5:
                continue
            
            if y1 < 0:      
                y1 = 0
            elif y1 > 255:
                y1 = 255
            y1 = str(y1)
            
            if y2 < 0:
                y2 = 0
            elif y2 > 255:
                y2 = 255
            y2 = str(y2)
                
            if y3 < 0:
                y3 = 0
            elif y3 > 255:
                y3 = 255
            y3 = str(y3)
                
            if y4 < 0:
                y4 = 0
            elif y4 > 255:
                y4 = 255
            y4 = str(y4)
            
            newline = x1 + ',' + y1 + ',' + x2 + ',' + y2 + ','+ x3 + ','+ y3 + ','+ x4 + ','+ y4 + ',' + 'A'+'\n'
            newlines.append(newline)
        
        new_labelname = label_savedir2 + labelname
        with open(new_labelname, "a") as f2:         #把坐标和标签内容写入txt文件
            for ns in newlines:
                f2.write(ns)


def check_verticles(imgdir,labeldir,out_dir):
    '''
    用来检查坐标是否超出图片的大小范围，
    以及检查是否有的图片丢失标签文件。
    '''
    #若标签数多于图片数，
    #可以遍历标签名，把标签名转化图片名，
    #使用if not (img_names == imgname).any() 进行判断

    if not os.path.exists(out_dir):                 #创建保存目录
        os.makedirs(out_dir)
    imgnames = np.array(os.listdir(imgdir))
    
    label_names = np.array(os.listdir(labeldir))         #获取所有标签文件名
    num=0
    hmaxs =[]
    print('Waiting..... checking....')
    for imgname in imgnames:
        imgpath = imgdir + imgname
        img = cv2.imread(imgpath)
        w = np.array(img).shape[1]                       #宽高用来判断坐标是否超出图片范围，从而导致训练出错
        h = np.array(img).shape[0]
        num += 1                                         #用来计数
        labelname = 'gt_' + imgname.split('.')[0] + '.txt'  #把图片名转换成标签名
        #labelname = imgname.split('.')[0] + '.txt'  #把图片名转换成标签名
        #print(labelname)
        
        '''#判断标签数是否少于图片数'''
        if not (label_names == labelname).any():         
            print('no label-->labelname:',labelname,' False')
        
        label2 = labeldir + labelname
        if (label_names == labelname).any():
            with open(label2, "r") as f1:                #读取标签文件的坐标和标签
                lines = f1.readlines()
            vertices, label3 = extract_vertices(lines)   #提取坐标
            lines2 = np.array(lines)
            if lines2.shape[0] == 0:                     #检查标签文件内容是否为空
                print('labelname--','shape wrong:',labelname,':',lines2.shape[0])
            '''把坐标取整'''
            for v1 in vertices:
                if v1 is None:
                    print('None')
                    break
                x1 = round(v1[0])
                y1 = round(v1[1])
                x2 = round(v1[2])
                y2 = round(v1[3])
                x3 = round(v1[4])
                y3 = round(v1[5])
                x4 = round(v1[6])
                y4 = round(v1[7])
                
                y = [y1,y2,y3,y4]
                ymin = min(y)
                ymax = max(y)
                hmax = ymax - ymin
                hmaxs.append(hmax)
                
                if x1<0 or x2<0 or x3<0 or x4<0 or y1<0 or y2<0 or y3<0 or y4<0:
                    print('v_wrong labelname:',labelname,' False')
                if x1>w or x2>w or x3>w or x4>w:
                    print('v_wrong labelname:',labelname,' False')
                if y1>h or y2>h or y3>h or y4>h:
                    print('v_wrong labelname:',labelname,' False')
                
                '''用直线连接 绘制四边形'''
                '''【注意box的格式】'''
                box = np.array([[x1,y1],[x2,y2],[x3,y3],[x4,y4]],np.int32)
                box = box.reshape(-1,1,2)
                cv2.polylines(img, [box], True,color=(0, 255, 0),thickness=2)
            #img = cv2.resize(img,(500,500))
            outname = out_dir + 'gt_' + imgname.split('.')[0] + '.jpg'
            cv2.imwrite(outname,img)
    if np.array(hmaxs).size != 0:
        print('h:',min(hmaxs))
'''----------------------------------------------------------------------------------------------------------------'''        
def remove_check_img(o_imgdir, check_imgdir):
    '''删去check文件夹下没有的原图'''
    o_imgnames = os.listdir(o_imgdir)
    c_imgnames = os.listdir(check_imgdir)
    for imgname in o_imgnames:
        check_img = 'gt_' + imgname
        if not check_img in c_imgnames:
            img = o_imgdir + imgname
            os.remove(img)

def remove_res_label(o_imgdir, c_labeldir):
    '''删除多余的标签'''
    labelnames = os.listdir(c_labeldir)
    imgnames = os.listdir(o_imgdir)
    
    for labelname in labelnames:
        imgname = (labelname.replace('gt_','')).replace('.txt','.jpg')
        if not imgname in imgnames:
            label = c_labeldir + labelname
            os.remove(label)
    
def remove_res_img(labeldir,imgdir):
    '''删除没有目标的图片'''
    labelnames = os.listdir(labeldir)
    imgnames = os.listdir(imgdir)
    
    for imgname in imgnames:
        labelname = 'gt_' + imgname.replace('.jpg','.txt')
        if not labelname in labelnames:
            img = imgdir + imgname
            os.remove(img)




'''----------------------------------main 函数----------------------------------------'''
if __name__ == '__main__':
    root_dir = 'D:/temp_data/code/dataset_tool/'
    o_img_root= root_dir + 'origin_data/image/'                   #原始图片地址
    o_label_root = root_dir + 'origin_data/label/'              #原始标签地址
    save_img_root = root_dir + 'done_data/crop_img/'          #新图片保存地址
    save_label_root = root_dir + 'done_data/crop_label/'      #新标签保存地址
    check_dis_dir = root_dir + 'check/'                   #验证图片是否正确的函数输出地址
    times_to_crop = 2
    thread = 0.3
    check_img_dir = save_img_root + 'mode3/'
    check_label_dir = save_label_root + 'mode3/'
    
    
    #verticle_repair(save_label_dir1,save_label_dir2)
    #remove_wrong_img(save_label_dir2,save_img_dir)
    
    
#    check_img_dir, check_label_dir = mode1_crop(o_img_root,o_label_root,save_img_root,save_label_root,times_to_crop,thread)    #模式一 裁剪， 未完成优化               
#    check_img_dir, check_label_dir = mode4_crop(o_img_root,o_label_root,save_img_root,save_label_root,thread,times_to_crop =1) #模式四 裁剪， 未完成优化                     
    
#    check_img_dir, check_label_dir = mode2_crop(root_dir,o_img_root,o_label_root,save_img_root,save_label_root,times_to_crop,thread) #模式二 裁剪， 未完成优化           
#    check_img_dir, check_label_dir = mode3_crop(root_dir,o_img_root,o_label_root,save_img_root,save_label_root,times_to_crop,thread) #模式三 裁剪， 未完成优化          

#    check_img_dir, check_label_dir = simple_crop(o_img_root,o_label_root,save_img_root,save_label_root,thread,times_to_crop =1)      #粗暴式裁剪
    
    '''*******检查图片是否出错********'''
#    check_verticles(check_img_dir,check_label_dir,check_dis_dir)
    
    o_imgdir = 'D:/temp_data/code/dataset_tool/done_data/crop_img/mode2/'
    check_imgdir = 'D:/temp_data/code/dataset_tool/check_mode3/'
    c_labeldir = 'D:/temp_data/code/dataset_tool/done_data/crop_label/mode2/'
    #remove_check_img(o_imgdir, check_imgdir)
    #remove_res_label(o_imgdir, c_labeldir)
    #remove_res_img(c_labeldir,o_imgdir)
























































